package com.raven.craft.code.generator.project.testcase;

import com.raven.craft.code.generator.IGenerator;
import com.raven.craft.code.generator.common.constant.FileSuffix;
import com.raven.craft.code.generator.common.file.HbsFile;
import com.raven.craft.code.generator.common.file.metadata.FileMetadata;
import com.raven.craft.code.generator.common.file.metadata.JavaFileMetadata;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * @author raven
 * @date 2020/08/29
 **/
@Slf4j
@Getter
@Setter
@ToString
@ConditionalOnProperty(prefix = "generator.project.test-case", name = "enabled", havingValue = "true", matchIfMissing = true)
@Component
public class TestCaseGenerator implements IGenerator {

    private TestCaseProperties properties;

    public TestCaseGenerator(TestCaseProperties properties) {
        this.properties = properties;
    }

    private static HbsFile createCaseListFile(TestCaseProperties properties, TestCaseVo testCaseVo) {
        FileMetadata metadata = new FileMetadata(testCaseVo.getName(), FileSuffix.MARKDOWN, properties.getTargetDir());
        return new HbsFile(metadata, properties.getCaseListTemplatePath(), testCaseVo);
    }

    private static HbsFile createTestCaseFile(TestCaseProperties properties, TestCaseVo testCaseVo) {
        FileMetadata metadata = new JavaFileMetadata(testCaseVo.getName(), properties.getTargetDir(), properties.getTargetPackage());
        return new HbsFile(metadata, properties.getTestCaseTemplatePath(), testCaseVo);
    }

    @Override
    public void generate() {
        log.info("begin to generate test case file");
        TestCaseVo testCaseVo = new TestCaseVo(properties);
        createCaseListFile(properties, testCaseVo).output();
        createTestCaseFile(properties, testCaseVo).output();
    }
}
